package com.fingard.dsp.bank.directbank.tjcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class TJCB01Balance extends TJCB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        int successCount = 0;
        StringBuilder messageStr = new StringBuilder();
        try {
            while (p_req7506.readRecord()) {
                TJCB01Header tjcb01Header = new TJCB01Header();
                tjcb01Header.transName = "B2eQueryBalance";
                tjcb01Header.clientTime = Format.DateTime2Format.format(new Date());
                tjcb01Header.batchID = p_req7506.reqHeader.transSeqID;
                tjcb01Header.transCount = "1";
                //发银行报文的XML对象
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, tjcb01Header);
                xmlWriter.writeStartElement("RequestBody");
                xmlWriter.writeStartElement("RequestRecord");
                xmlWriter.writeElementString("AcctNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_req7506.reqHeader.transType, p_req7506.reqHeader.transType);

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmprspcod = tmpBkReader.getFirstText("/transaction/RespondHeader/respondCode");
                        String tmprspmsg = tmpBkReader.getFirstText("/transaction/RespondHeader/respondInfo");
                        if (tmprspcod != null && "0000".equals(tmprspcod)) {
                            successCount++;
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmprspmsg;
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                            //当前余额
                            String tmpCurrBal = tmpBkReader.getFirstTextNotNull("/transaction/RespondBody/RespondRecord/Balance");
                            tmpRetDetail.currBal = StringHelper.isNullOrEmpty(tmpCurrBal) ? "0.00" : tmpCurrBal;
                            //可用余额
                            String tmpAvailBal = tmpBkReader.getFirstTextNotNull("/transaction/RespondBody/RespondRecord/AvailableBalance");
                            tmpRetDetail.availBal = StringHelper.isNullOrEmpty(tmpAvailBal) ? "0.00" : tmpAvailBal;
                            //冻结余额
                            String tmpFrozenBal = tmpBkReader.getFirstTextNotNull("/transaction/RespondBody/RespondRecord/LockBalance");
                            tmpRetDetail.frozenBal = StringHelper.isNullOrEmpty(tmpFrozenBal) ? "0.00" : tmpFrozenBal;
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        } else {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmprspmsg;
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        messageStr.append("账号:").append(p_req7506.readedDetail.actNum).append(",查询余额失败：").append(tmpSbMsg).append(";\r\n");
                    }
                } else {
                    messageStr.append("账号:").append(p_req7506.readedDetail.actNum).append(",查询余额失败：").append(tmpStrRet[0]).append(";\r\n");
                }
            }
            if (successCount > 0) {
                retAts.writeRespCode("0");
                if (messageStr.toString().length() > 0) {
                    retAts.writeRespInfo(messageStr.toString());
                } else {
                    retAts.writeRespInfo("查询成功");
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(messageStr.toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            messageStr.append("查询异常：").append(ex.getMessage()).append("\r\n");
            retAts.writeRespInfo(messageStr.toString());
        } finally {
            if (messageStr.length() > 0) {
                WriteBankLogLn(messageStr.toString());
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
